package com.zerui.manager.app.accessauthorg.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zerui.basedata.client.OrganizationService;
import com.zerui.basedata.client.cmd.OrganizationQueryPageCmd;
import com.zerui.basedata.client.dto.OrganizationDTO;
import com.zerui.cola.dto.MultiResponse;
import com.zerui.cola.dto.PageResponse;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.accessauthorg.gateway.AccessAuthOrgGateWay;
import com.zerui.manager.web.cmd.AccessAuthOrgQryCmd;
import com.zerui.manager.web.vo.AccessAuthOrgPageVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-29 17:28
 */
@Component
@CatchAndLog
public class AccessAuthOrgPageCmdExe {

    @DubboReference
    private OrganizationService organizationService;
    @Resource
    private AccessAuthOrgGateWay accessAuthOrgGateWay;

	public PageResponse<AccessAuthOrgPageVO> execute(AccessAuthOrgQryCmd accessAuthOrgQryCmd) {
        OrganizationQueryPageCmd cmd = new OrganizationQueryPageCmd();
        cmd.setParentOrgId(accessAuthOrgQryCmd.getParentOrgId());
        cmd.setBizType("department");
        cmd.setPageIndex(accessAuthOrgQryCmd.getPageIndex());
        cmd.setPageSize(accessAuthOrgQryCmd.getPageSize());
        PageResponse<OrganizationDTO> pageResponse = organizationService.pageOrganization(cmd);

        List<AccessAuthOrgPageVO> result = new ArrayList<>(16);

        if(ObjectUtils.isNotEmpty(pageResponse)){
            List<OrganizationDTO> data = pageResponse.getData();

            List<Long> orgIds = data.stream()
                    .map(OrganizationDTO::getId)
                    .collect(Collectors.toList());

            Map<Long,Long> orgMap =  accessAuthOrgGateWay.getAuthIdMapByOrgIds(orgIds);

            SingleResponse<Map<Long,Integer>> response= organizationService.getPersonNumMapByOrgIds(orgIds);
            Map<Long,Integer> orgNum = response.getData();

            List<Long> parentIds = data.stream()
                    .map(OrganizationDTO::getParentOrgId)
                    .collect(Collectors.toList());

            Map<Long,String> parentMap = new HashMap<>(16);

            MultiResponse<OrganizationDTO> organization = organizationService.listOrganization(parentIds);
            if(ObjectUtils.isNotEmpty(organization) && CollectionUtils.isNotEmpty(organization.getData())){
                parentMap = organization.getData()
                        .stream()
                        .collect(Collectors.toMap(OrganizationDTO::getId,OrganizationDTO::getOrgName));

            }
            Map<Long, String> finalParentMap = parentMap;
            data.forEach(e-> result.add(AccessAuthOrgPageVO.builder()
                    .accessAuthId(orgMap.get(e.getId()))
                    .num(orgNum.get(e.getId()))
                    .orgName(e.getOrgName())
                    .parentOrgId(e.getParentOrgId())
                    .parentOrgName(finalParentMap.get(e.getParentOrgId()))
                    .id(e.getId())
                    .build()));
        }
        return PageResponse.of(result,pageResponse.getTotalCount(),
                 pageResponse.getPageSize(),
                 pageResponse.getPageIndex());
    }
}
